package com.qf.swagger.controller;

import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@RestController
@Slf4j
public class DownloadController {
    @Value("file.upload.path")
    String fileUploadPath;
    @GetMapping("/download")
    public void download(String filename , HttpServletResponse response) throws IOException {
        log.debug("下载导出的文件：{}",filename);
        //定位要下载文件的绝对路径
        final String fileUploadPath = this.fileUploadPath+filename;

        //让浏览器以附件形式下载文件
        response.setHeader("content-disposition","attachment;filename="+filename);
        //把文件内容写入响应对象的输出流中
        IoUtil.copy(new FileInputStream(fileUploadPath),response.getOutputStream());
    }
}
